#!/usr/bin/env python
#
# Copyright 2008 Allen Hutchison (allen@hutchison.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# $URL$
__version__ = "$Rev: 31 $"
__author__ = "Allen Hutchison (allen@hutchison.org)"

"""Unit tests for TubeParser"""

import unittest

from lib.scrapers.uk.lon.tube_parser import TubeParser

class TubeParserTest(unittest.TestCase):
  def setUp(self):
    self.lines = ['jubilee', 'bakerloo', 'central', 'metropolitan', 'district',
                  'piccadilly', 'hammersmithandcity', 'victoria', 'circle',
                  'northern', 'waterlooandcity']
    self.statuses = ['goodservice', 'minordelays', 'severedelays', 
                     'partsuspended', 'suspended', 'partclosure', 
                     'plannedclosure']
    self.t = TubeParser()
  
  def test_url(self):
    url = "http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/default.html"
    self.assertEquals(url, self.t.url, "URL is wrong %s" % self.t.url)
  
  def test_getContent(self):
    self.assert_(self.t.status is not {}, "Content is empty")
  
  def test_getLinesNoUnexpectedLines(self):
    failMsg = "%s isn't an acceptable line name"
    for x in self.t.getLines():
      self.assert_(x in self.lines, failMsg % x)
  
  def test_getLinesHasCorrectNumberOfLines(self):
    self.assertEquals(11, len(self.t.getLines()))
  
  def test_getLinesHasNoDuplicates(self):
    failMsg = "%s in list %d times should be 1"
    test = {}
    for x in self.t.getLines():
      if test.has_key(x):
        test[x] += 1
      else:
        test[x] = 1
    for x in test:
      self.assertEquals(1, test[x], failMsg % (x, test[x]))
  
  def test_getStatus(self):
    failMsg = "Status isn't in the list of acceptable statuses: %s"
    for x in self.t.getLines():
      self.assert_(self.t.getStatus(x) in self.statuses, 
                   failMsg % self.t.getStatus(x))
  
